/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


package bw.note;

import java.util.*;
import java.io.*;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.*;
import bw.util.*;
import bw.os.*;

public class NoteManager
{
    private static final String NOTES_KEY = "WD_NOTE_";

    private String _name = null;
    private String _key = null;

    public NoteManager ( String name )
    {
        _name = name;
        _key = NOTES_KEY + name;
    }

    public Note createNote ( Date d )
    {
        Note note = new Note( d );
        note.setId( createId() );
        
        notes().addElement( note );

        return note;
    }

    public Note createNote ()
    {
        return createNote( new Date() );
    }

    public void storeNotes ()
        throws NoteException 
    {
        //convenience
        storeNotes( notes() );
    }

    public void deleteNote ( Note note )
        throws NoteException
    {
        Vector notes = notes();

        notes.remove( note );

        storeNotes( notes );
    }

    public Vector getNotes ()
    {
        return notes();
    }

    public int size ()
    {
        return notes().size();
    }

    public Note getNote ( String id )
    {
        Note note = null;

        for ( Enumeration e = notes().elements(); e.hasMoreElements(); )
        {
            Note n = (Note) e.nextElement();
            if ( n.getId().equals( id ) )
            {
                note = n;
                break;
            }
        }

        return note;
    }

    public Vector getNotesForDates ( Date start, Date end )
    {
        Vector notes = new Vector();

        for ( Enumeration e = notes().elements(); e.hasMoreElements(); )
        {
            Note n = (Note) e.nextElement();
            if ( ( start.equals( n.getDate() ) || start.before( n.getDate() ) ) && 
                 ( end.equals( n.getDate() ) || end.after( n.getDate() ) ) )
            {
                notes.addElement( n );
            }
        }

        return notes;
    }

    public void destroy ()
    {
        try
        {
            ObjectStore.getInstance().remove( _key );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem removing notes!", ex );
        }
    }

    private Vector notes()
    {
        Vector n = null;
        try
        {
            n = (Vector) ObjectStore.getInstance().get( _key );

            if ( n == null )
            {
                n = new Vector();
                ObjectStore.getInstance().put( _key, n );
            }
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem getting notes!", ex );
            if ( n == null ) { n = new Vector(); }
        }

        Collections.sort( n );

        return n;
    }

    private void storeNotes ( Vector notes )
    {
        try
        {
            ObjectStore.getInstance().put( _key, notes );
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "NoteManager: Problem storing notes for " + _key, ex );
        }
    }
    
    private String createId()
    {
        String id = null;

        //is this enough to be unique?
        id = String.valueOf( System.currentTimeMillis() + "-" + 
                             Thread.currentThread().getName() );

        return id;
    }
}
